import { RequestHandler } from 'express';
import { req_info_increase_type } from '../../../../../../type/api/increase_type/Classes/cla_req_info_increase_type';
import { check_only_set_cur_increase_self_type } from '../Func/Sign_info/Func/check_only_set_cur_increase_self_type';
import { res_send_cur_type_name_is_occupy } from '../../../../../../type/api/increase_type/Classes/cla_res_send_cur_type_name_is_occupy';
import { get_db_cur_user_self_type } from '../Func/Sign_info/Func/get_db_cur_user_self_type';
import { get_cur_db_count_for_id } from '../../../../../../../Func/get_mdb_new_id';
import { full_id } from '../../../../../../../../../Func/full_id';
import { spacename_self_type } from '../DataDefine/sourceData';
import { res_send_increase_type_done } from '../../../../../../type/api/increase_type/Classes/cla_res_send_increase_type_done';
import { check_only_set_cur_increase_type } from '../../../../../../type/api/increase_type/Func/Sign_info/Func/check_only_set_cur_increase_type';
import { res_send_cur_self_type_name_is_occupy } from '../Classes/cla_res_send_cur_self_type_name_is_occupy';
import { sign_increase_self_type } from '../Func/Sign_info';
import { log_cur_user_increase_self_type } from '../Func/Log_info';
import { spacename_user } from '../../../../../DataDefine/sourceData';
import { prop_cur_req_user_identity } from '../../../../../../../Classes/cla_req_cur_headers_info';
/**
 * 创建 一个 私有 类型 定义
 */
export const increase_self_type: RequestHandler = async (req, res) => {
  let cur_info = new req_info_increase_type(req);

  let golbal_only_set = await check_only_set_cur_increase_type(cur_info);

  /**
   * golbal_only_set 为 false  时 说明 全局 类型表 已经 存在 该条记录
   */
  if (!golbal_only_set) {
    // 发送 当前上传类型名 已经 被 占用
    let res_send = new res_send_cur_type_name_is_occupy(cur_info);

    res.send(res_send);
    return;
  }

  let only_set = await check_only_set_cur_increase_self_type(cur_info);

  /**
   * only_set 为 false  时 说明 私有 类型表 已经 存在 该条记录
   */
  if (!only_set) {
    // 发送 当前上传类型名 已经 被 占用
    let res_send = new res_send_cur_self_type_name_is_occupy(cur_info);

    res.send(res_send);
    return;
  }

  /**
   * 获取 id 编号full_id
   */
  let cur_self_type_id_num = await get_cur_db_count_for_id(
    get_db_cur_user_self_type(cur_info)
  );

  /**
   * 完整 编号
   */
  cur_info.id = full_id(
    spacename_user,
    cur_info[prop_cur_req_user_identity],
    spacename_self_type,
    cur_self_type_id_num
  );

  log_cur_user_increase_self_type(cur_info);

  // 添加 表信息
  sign_increase_self_type(cur_info);

  // 发送 添加 成功
  let res_send = new res_send_increase_type_done(cur_info);

  res.send(res_send);
};
